﻿define(function (require, exports, module) {
    var utility = require('ezdesk/utility/utility.js');

    return Backbone.View.extend({
        POPUP_DISPLAY_TIME: 5000, // 弹出框显示时间，单位ms

        el: $("#notification"),
        tagName: 'div',
        template: _.template($("#tpl-notification").html()),
        events: {
            'click .func-close': 'hide',
            'click .func-prev': 'prev',
            'click .func-next': 'next',
            'mouseover #notification-panel': 'panelMouseover',
            'mouseout #notification-panel': 'panelMouseout',
        },
        currentFeedIndex: -1,
        timerId: null,
        initialize: function () {
            $(this.el);
            _.bindAll(this, 'render', 'addOne', 'hide', 'popup', 'prev', 'next');
            this.model.bind('add', this.addOne);
            this.model.bind('add', this.render);
            this.model.view = this;
        },
        addOne: function (e) {
            var count = this.model.models.length;
            var index = 0;
            _.each(this.model.models, function (e) {
                // 第一条feed 的pager应该是最新的。。
                e.set({ PaggerText: (index + 1)  + '/' + count });
                index++;
            });
            this.currentFeedIndex = 0;
        },
        render: function () {
            var _self = this;
            var element = $(this.el);
            var count = this.model.models.length;
            
            if (this.currentFeedIndex == -1) return;
            var _model = this.model.models[this.currentFeedIndex];               
            
            $(element).html(this.template(_model.toJSON()));
                
            if (_model.get('IsRead')) {
                $(element).find('.title').addClass('is-read');
            }

            var feedId = _model.get('FeedId');
            $(element).find('.body a.ezlink').each(function (index, item) {
                $(item).appendTo($(element).find(".action"));
                utility.HandleATag(item,null, feedId);
            });
            $(element).find('.title a.ezlink').each(function (index, item) {
                utility.HandleATag(item,null, feedId);
            });

            $(this.el).find('.func-prev, .func-next').show();
            
            if (count == 1) {
                $(this.el).find('.page').hide();
            }

            $(this.el).find('#notification-view').hide().show();

            this.popup();
            return this;
        },
        hide: function () {
            clearTimeout(this.timerId);
            $.postMessage('notification-hide', '*', parent);
            // 清空弹出的feeds弹出框，并关闭弹出框
            this.model.reset();
        },
        next: function () {
            var count = this.model.models.length;
            if (this.currentFeedIndex == count - 1) return;
            this.currentFeedIndex ++ ;
            this.render();
        },
        prev: function () {
            if (this.currentFeedIndex == 0) return;
            this.currentFeedIndex -- ;
            this.render();
        },
        popup: function () {
            var _self = this;
            console.log('now, popup...');
            var thisHeight = $(this.el).height();

            $.postMessage('notification:' + thisHeight, '*', parent);

            clearTimeout(this.timerId);
            this.timerId = setTimeout(function () {
                _self.hide();
            }, this.POPUP_DISPLAY_TIME);
        },
        panelMouseover: function () {
            var _self = this;
            clearTimeout(this.timerId);
        },
        panelMouseout: function () {
            var _self = this;
            clearTimeout(this.timerId);
            this.timerId = setTimeout(function () {
                _self.hide();
            }, this.POPUP_DISPLAY_TIME - 3000);
        },
    });
});